package com.zerosky.crm.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zerosky.crm.po.Customer;
import com.zerosky.crm.po.Employee;
import com.zerosky.crm.po.SharingRecords;
import com.zerosky.crm.service.EmployeeService;
import com.zerosky.crm.service.SharingRecordsService;
import com.zerosky.crm.util.BaseAction;
import com.zerosky.crm.util.PageInfo;

/**
 * 分享/浏览功能的Controller
 * 
 * @author 周正祥
 *
 */
@Controller
public class SharingRecordsController extends BaseAction {
	private Logger logger = LoggerFactory.getLogger(SharingRecordsController.class); // 日志

	@Resource(name = "sharingRecordsService")
	private SharingRecordsService sharingRecordsService;

	@Resource(name = "employeeService")
	private EmployeeService employeeService;

	/**
	 * 获取分享/浏览列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/sharingRecordsList.do")
	@ResponseBody
	public Map<String, Object> TrainList(HttpServletRequest request, HttpSession session, HttpServletResponse response,
			Integer limit, Integer page, String sharingRecordsName, Integer activityId) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>(); // 定义返回map集合
		Map<String, Object> param = new HashMap<String, Object>();// 定义条件集合
		try {
			param.put("page", page);
			param.put("pageSize", limit);
			if (Strings.isNotEmpty(sharingRecordsName)) {
				param.put("sharingRecordsName", sharingRecordsName);
			}
			param.put("contentId", activityId);
			param.put("contentType", "1");
			PageInfo<SharingRecords> pageRole = sharingRecordsService.getSharingRecordsList(param);
			json.put("total", pageRole.getPageCount());
			json.put("data", pageRole.getList());
			json.put("code", "0");
		} catch (Exception e) {
			logger.error("获取数据失败，原因：" + e);
			json.put("code", "-1");
		}
		return json;

	}

	

	/**
	 * 添加分享/浏览记录
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/insertSharingRecords.do")
	@ResponseBody
	public Map<String, Object> insertVisit(SharingRecords SharingRecords, HttpServletRequest request, HttpSession session,
			HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			//http://3h86j6k.hk1.mofasuidao.cn/FUNDCRM/wap/market_detailed_body.do?contentId=86&empId=0d9f03fd-2f96-4abd-81f4-18904da60bfe&contentType=0&sharingType=1
			Employee user = new Employee(); //员工
			if ((Employee) session.getAttribute("loginUser")!=null||request.getParameter("empId")!=null) {
				if (request.getParameter("empId")!=null) {
					user = employeeService.getEmployeeById(request.getParameter("empId"));
				}else
				{
					user = (Employee) session.getAttribute("loginUser");// 获取登录用户
				}
			} 
			
			//浏览记录，记录每一个客户id
 			if (SharingRecords.getSharingType().equals("1")) {
 				Customer customer = (Customer) session.getAttribute("loginCustomer");// 获取登录客户 
 				SharingRecords.setCustomerId(customer.getCustomerId()); 
			} else if (SharingRecords.getSharingType().equals("0")) {
				SharingRecords.setEmpId(user.getEmployeeId());
			} 
 			
 			if (sharingRecordsService.getSharingRecordsList(json).getPageCount()==0) {
 				//新增操作
 				String StrId = UUID.randomUUID().toString();
 	 			SharingRecords.setSharingDate(new Date());
 				SharingRecords.setSharingId(StrId);
 				SharingRecords.setStatus("0");
 	  			
 				sharingRecordsService.saveSharingRecords(SharingRecords, "新增");
 				json.put("code", 0); 
			}else {
				json.put("code", 1);
			} 
		} catch (Exception e) {
			json.put("code", -1);
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}

	

	/**
	 * 新增分享记录
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/insertCustomerSharingRecordsToId.do")
	@ResponseBody
	public Map<String, Object> insertCustomerSharingRecordsToId(SharingRecords sharingRecords,
			HttpServletRequest request, HttpSession session, HttpServletResponse response) {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			Employee user = (Employee) session.getAttribute("loginUser");// 获取登录用户
			String StrId = UUID.randomUUID().toString();
			sharingRecords.setSharingId(StrId);//生成UUID
			sharingRecords.setSharingDate(new Date());//获取当前时间
			sharingRecords.setStatus("0");//设置有效记录
			sharingRecords.setEmpId(user.getEmployeeId());//写入员工ID
			sharingRecordsService.saveSharingRecords(sharingRecords, "新增");
			json.put("code", 0);
		} catch (Exception e) {
			json.put("code", -1);
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}

	/**
	 * 删除分享/浏览记录
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/deleteCustomerSharingRecordsToId.do")
	@ResponseBody
	public Map<String, Object> deleteCustomerSharingRecordsToId(String customerId, HttpServletRequest request,
			HttpSession session, HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			sharingRecordsService.deleteSharingRecordsById(customerId);
			json.put("code", 0);
		} catch (Exception e) {
			json.put("code", -1);
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}
	/**
	 * 获取分享/浏览列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/employeeSharingRecordsList.do")
	@ResponseBody
	public Map<String, Object> employeeSharingRecordsList(HttpServletRequest request, HttpSession session, HttpServletResponse response,
			Integer limit, Integer page, Integer activityId,String employeeName) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>(); // 定义返回map集合
		Map<String, Object> param = new HashMap<String, Object>();// 定义条件集合
		try {
			param.put("page", page);
			param.put("pageSize", limit);
			if (Strings.isNotEmpty(employeeName)) {
				param.put("employeeName", employeeName);
			}
			param.put("contentId", activityId);
			param.put("contentType", "1");
			PageInfo<SharingRecords> pageRole = sharingRecordsService.getEmployeeRecordsList(param);
			json.put("total", pageRole.getPageCount());
			json.put("data", pageRole.getList());
			json.put("code", "0");
		} catch (Exception e) {
			logger.error("获取数据失败，原因：" + e);
			json.put("code", "-1");
		}
		return json;

	}
}
